//
//  ST_RegisterFriendForViewController.swift
//  btvideo
//
//  Created by Jason on 2021/12/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftUI
import AudioToolbox

class RegisterFriendForCellModel {
    var image: UIImage?
    var isSelect = false
    var id = 0
}

/// 交友模式
class ST_RegisterFriendForViewController: UIViewController, ST_RegisterRouteProtocol {

    var imageArray: [RegisterFriendForCellModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        make_u_i()
        
        for index in 0...1 {
            let model = RegisterFriendForCellModel()
            model.image = index == 0 ? ST_ImageUtils.imageName("friend_for_1") : ST_ImageUtils.imageName("friend_for_2")
            model.isSelect = false
            imageArray.append(model)
        }
        
        collectionView.reloadData()
        
        back_thread_execute(afterDelay: 1) {
            ST_RegisterMonitor.shared.logger.report(fromController: self)
        }
    }
    
    fileprivate func make_u_i() {
        view.addSubviews([progress_view, titleLabel, sub_title_label, collectionView, next_button])
        
        progress_view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight + 25)
            make.height.equalTo(2)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(progress_view.snp.bottom).offset(26)
            make.height.equalTo(24)
        }
        
        sub_title_label.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.height.equalTo(20)
        }
        
        next_button.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-46)
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(40)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(sub_title_label.snp.bottom).offset(33)
            make.bottom.equalTo(next_button.snp.top).offset(0)
        }
    }
    
    @objc func goNext() {
        pushNextRegisterController(parameters: nil)
    }

    // MARK: - UI
    fileprivate lazy var progress_view: registerProgressView = {
        let v = registerProgressView()
//        v.setProgress(all: 4, p: 0)
        v.setProgress(currentIndex: 0, amount: 4)
        return v
    }()
    
    fileprivate lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.text = "你想要的交友模式是？"
        l.textColor = UIColor.init(hex: 0x333333)
        l.font = .systemFont(ofSize: 17, weight: .medium)
        return l
    }()
    
    fileprivate lazy var sub_title_label: UILabel = {
        let l = UILabel()
        l.text = "我们会帮你找到符合你期望的人"
        l.textColor = UIColor.init(hex: 0x999999)
        l.font = .systemFont(ofSize: 14, weight: .regular)
        return l
    }()
    
    fileprivate lazy var collectionView: UICollectionView = {
        let f = UICollectionViewFlowLayout()
        f.minimumLineSpacing = 10
        f.itemSize = CGSize(width: ScreenWidth, height: TIANCompatibleValue(value: 156))
        
        let c = UICollectionView(frame: .zero, collectionViewLayout: f)
        c.backgroundColor = .white
        c.delegate = self
        c.dataSource = self
        c.register(cellWithClass: RegisterFriendForCollectionViewCell.self)
        return c
    }()
    
    fileprivate lazy var next_button: UIButton = {
        let b = UIButton()
        b.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)

        b.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xDFE3E9)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.disabled)
        b.setTitleForAllStates("下一步")
        b.setTitleColorForAllStates(.white)
        b.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        b.cornerRadius = 20
        b.isEnabled = false
        b.addTarget(self, action: #selector(goNext), for: .touchUpInside)
        return b
    }()
}

extension ST_RegisterFriendForViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: RegisterFriendForCollectionViewCell.self, for: indexPath)
        cell.bind(data: imageArray[safe: indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        for index in 0 ..< imageArray.count {
            imageArray[index].isSelect = false
        }
        imageArray[indexPath.row].isSelect = true
        next_button.isEnabled = true
        collectionView.reloadData()
    }
}

class RegisterFriendForCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        make_u_i()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func make_u_i() {
        contentView.addSubviews([cellImageView, selectImageView])
        
        cellImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.bottom.equalToSuperview()
        }
        
        selectImageView.snp.makeConstraints { make in
            make.right.equalTo(cellImageView).offset(-20)
            make.bottom.equalTo(cellImageView).offset(-20)
            make.size.equalTo(CGSize(width: 28, height: 28))
        }
    }
    
    func bind(data: RegisterFriendForCellModel?) {
        guard let safeData = data else {
            return
        }
        cellImageView.image = safeData.image
        selectImageView.isHidden = !safeData.isSelect
    }
    
    // MARK: - UI
    fileprivate lazy var cellImageView: UIImageView = {
        let i = UIImageView()
        return i
    }()
    
    fileprivate lazy var selectImageView: UIImageView = {
        let i = UIImageView()
        i.image = ST_ImageUtils.imageName("friendfor_choose")
        i.isHidden = true
        return i
    }()
}
